package com.tnsw.noxnox;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.noxnox.db.DBManager;
import com.tnsw.noxnox.db.DBManagerInterface;
import com.tnsw.noxnox.db.tables.DownloadProgress;
import com.tnsw.noxnox.dialog.MyFragmentDialog1;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.text.AndroidCharacter;
import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.SearchView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CatalogFragment extends FragmentWithAdapter implements DBManagerInterface, OnItemClickListener {
	
	private String tag = "CatalogFragment";
	private DBManager dbm;
	private AsyncManager asyncManager;
	
	private ViewGroup root;
	private CatalogAdapter adapter;
	
	private CatalogDetailFragment newFragment = null;
	
	public CatalogFragment() {
		super();
		Log.d(tag, "Constructor()");
	}
	
	public CatalogFragment(Context context, FragmentPagerAdapter pagerAdapter) {
		super(context, pagerAdapter);
		Log.d(tag, "Constructor(context)");
		
		this.context = context;
		this.dbm = DBManager.getInstance(this.context);
		this.asyncManager = AsyncManager.getInstance();
		
		this.setHasOptionsMenu(true);
		
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
	}
	
	public static Fragment newInstance(Context context, FragmentPagerAdapter pagerAdapter) {
		Log.d("CatalogFragment", "newInstance()");
		CatalogFragment f = new CatalogFragment(context, pagerAdapter);
		return(f);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		Log.d(tag, "onResume()");
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//menu.clear();
		//inflater.inflate(R.menu.catalog, menu);
		//SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
		Log.d(tag, "onCreateOptionsMenu()");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(tag, "onCreateView()");
		this.root = (ViewGroup) inflater.inflate(R.layout.activity_catalog, null);
		
		ListView lv = (ListView) this.root.findViewById(R.id.list);
		this.adapter = new CatalogAdapter(this.context);
		lv.setAdapter(this.adapter);
		lv.setOnItemClickListener(this);
		
		return(root);
	}
	
	public void doRefresh() {
		if (this.adapter != null) {
			DBManager dbm = DBManager.getInstance(context);
			dbm.DBDispatcher.register(this);
			this.adapter.doRefresh();
			this.adapter.notifyDataSetChanged();
			Log.d(tag, "doRefresh()");
		} else {
			Log.d(tag, "adapter == null");
		}
	}

	@Override
	public void onSQLExecuted(DBManager sender, String command, String tableName) {
		boolean b = AsyncManager.isUiThread();
		//Log.d(tag, "command=" + command + ", tableName=" + tableName + " isUiThread=" + b);
		
		//this.adapter.doRefresh();
		//this.adapter.notifyDataSetChanged();
		if (command.equals("insert")) {
			this.adapter.doRefresh();
		} else if (command.equals("update")) {
			this.adapter.doRefreshBackground();
		}
		
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		//MyFragmentDialog1 d = new MyFragmentDialog1(this.context);
		//d.show(this.getFragmentManager(), "d1");

		
		/*
		Intent intent = new Intent(this.getActivity(), com.tnsw.noxnox.YoutubeTest.class);
		this.getActivity().startActivity(intent);
		*/

		DownloadProgress dp = (DownloadProgress) this.adapter.getItem(position);
		Log.d(tag, "dp.primarykey=" + dp.primarykey);
		
		FragmentManager manager = this.getFragmentManager();
		FragmentTransaction tran = manager.beginTransaction();
		
		//this.newFragment = new YoutubeFragment(this.context, super.adapter);
		this.newFragment = new CatalogDetailFragment(this.context, super.adapter, dp, this);
		//tran.add(android.R.id.content, newFragment);
		//tran.detach(this);
		tran.hide(this);
		
		tran.replace(android.R.id.content, newFragment);
		//tran.attach(newFragment);
		
		tran.commit();
		//this.getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		this.getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
		

	}
	
	public void returnHome() {
		if (this.newFragment != null) {
			FragmentManager manager = this.getFragmentManager();
			FragmentTransaction tran = manager.beginTransaction();
			
			tran.detach(this.newFragment);
			tran.show(this);
			tran.commit();
		}		
	}
	
	public void detachFromOutsite() {
		if (this.newFragment != null) {
			FragmentManager manager = this.getFragmentManager();
			FragmentTransaction tran = manager.beginTransaction();
			
			tran.detach(this.newFragment);
			tran.commit();
		}
	}
	

}
